package com.sls.web.manager.action.recharge;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.PriceTemplate;
import com.sls.web.manager.recharge.pojo.PriceTemplateSpecial;
import com.sls.web.manager.recharge.pojo.SystemProduct;
import com.sls.web.manager.recharge.querybean.PriceTemplateSpecialQuery;
import com.sls.web.manager.recharge.querybean.SystemProductQuery;
import com.sls.web.manager.recharge.service.PriceTemplateService;
import com.sls.web.manager.recharge.service.PriceTemplateSpecialService;
import com.sls.web.manager.recharge.service.SystemProductService;

/**
 * @author sxf
 * 
 */
public class PriceTemplateSpecialAction extends SuperAction {
	private static final long serialVersionUID = -4724066167403364279L;

	@Resource(name = "priceTemplateSpecialService")
	private PriceTemplateSpecialService service;
	@Resource(name = "priceTemplateService")
	private PriceTemplateService priceTemplateService;
	@Resource(name = "systemProductService")
	private SystemProductService systemProductService;

	private Pagination<PriceTemplateSpecial> page;
	private PriceTemplateSpecialQuery ptsQuery;
	private String templateCode;// 价格模板id
	private PriceTemplate pt;

	public String list() throws Exception {
		if (ptsQuery == null) {
			ptsQuery = new PriceTemplateSpecialQuery();
			ptsQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			ptsQuery.setPageSize(Constants.INIT_PAGESIZE);
		}
		if (StringUtils.isNotBlank(templateCode)) {
			ptsQuery.setPriceTempletCode(templateCode);
			pt = priceTemplateService.findById(templateCode,
					getCurrentOperator());
		}
		page = service.querySpecialPage(ptsQuery, getCurrentOperator());
		return "list";
	}

	public String doAddOrEdit() throws Exception {
		String id = WebTools.getParameter("id");
		String systemProductId = WebTools.getParameter("systemProductId");
		String channelAgentPrice = WebTools.getParameter("channelAgentPrice");
		String doorPrice = WebTools.getParameter("doorPrice");
		String doorAddPrice = WebTools.getParameter("doorAddPrice");
		if (StringUtils.isBlank(channelAgentPrice)) {
			channelAgentPrice = "";
		}
		if (StringUtils.isBlank(doorPrice)) {
			doorPrice = "";
		}
		if (StringUtils.isBlank(doorAddPrice)) {
			doorAddPrice = "";
		}
		String result = "success";
		String insertId = "";
		try {
			if (StringUtils.isBlank(id)) {
				SystemProductQuery spq = new SystemProductQuery();
				spq.setId(systemProductId);
				SystemProduct systemProduct = systemProductService.queryOne(
						spq, getCurrentOperator());
				Integer systemChannelAgentPrice = systemProduct
						.getChannelAgentPrice();
				Integer systemDoorPrice = systemProduct.getDoorPrice();
				Integer systemDoorAddPrice = systemProduct.getDoorAddPrice();
				if (!isEqual(channelAgentPrice, systemChannelAgentPrice)
						|| !isEqual(doorPrice, systemDoorPrice)
						|| !isEqual(doorAddPrice, systemDoorAddPrice)) {
					PriceTemplateSpecial pts = new PriceTemplateSpecial();
					pts.setSystemProductId(systemProductId);
					pts.setDeleted(false);
					pts.setEnable(true);
					pts.setFaceValue(systemProduct.getFaceValue());
					pts.setPriceTempletCode(templateCode);
					pts.setUpdateOperator(getCurrentOperator().getUserId());
					try {
						pts.setChannelAgentPrice((int) (Double
								.parseDouble(channelAgentPrice) * 1000));
					} catch (Exception e) {

					}
					try {
						pts.setDoorPrice((int) (Double.parseDouble(doorPrice) * 1000));
					} catch (Exception e) {

					}
					try {
						pts.setDoorAddPrice((int) (Double
								.parseDouble(doorAddPrice) * 1000));
					} catch (Exception e) {

					}
					insertId = service.insert(pts, getCurrentOperator());
				}
			} else {
				PriceTemplateSpecial pts = new PriceTemplateSpecial();
				pts.setId(id);
				try {
					pts.setChannelAgentPrice((int) (Double
							.parseDouble(channelAgentPrice) * 1000));
				} catch (Exception e) {

				}
				try {
					pts.setDoorPrice((int) (Double.parseDouble(doorPrice) * 1000));
				} catch (Exception e) {

				}
				try {
					pts.setDoorAddPrice((int) (Double.parseDouble(doorAddPrice) * 1000));
				} catch (Exception e) {

				}
				service.update(pts, getCurrentOperator());
			}
		} catch (Exception e) {
			result = "error";
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", result);
		map.put("id", insertId);
		message = WebTools.writeAjax(map);
		return "ajax";
	}

	private boolean isEqual(String s1, Integer i) {
		if (i == null || s1.equals((double) (i * 1.0 / 1000) + "")) {
			return true;
		}
		return false;
	}

	public PriceTemplateSpecialQuery getPtsQuery() {
		return ptsQuery;
	}

	public void setPtsQuery(PriceTemplateSpecialQuery ptsQuery) {
		this.ptsQuery = ptsQuery;
	}

	public Pagination<PriceTemplateSpecial> getPage() {
		return page;
	}

	public String getTemplateCode() {
		return templateCode;
	}

	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public PriceTemplate getPt() {
		return pt;
	}

	public void setPt(PriceTemplate pt) {
		this.pt = pt;
	}

}
